package com.itboye.textileexpres.login;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.textileexpes.presenter.GetYanzhengMaPresenter;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.TimeCount;

/**
 * 
 * @author 注册获取的验证码
 * 
 */
public class SMSSendActivity extends BaseActivity implements OnClickListener,
		TextWatcher, Observer {
	ImageView back;
	Button next_tv = null;
	TextView country_code;
	EditText phone_num = null;
	private TextView country_tv;
	String Country;
	String numBer;
	String phoneNumBer;
	String phone;
	RelativeLayout country_rel;
	TimeCount count;
	MainPresenter mainPresenter;
	GetYanzhengMaPresenter getYanzhengMaPresenter;
	private String code;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_sms_send;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		phone_num.addTextChangedListener(this);
		count = new TimeCount(60000, 1000, next_tv);
		onReigSMSS();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		if (phone_num.getText().toString().length() > 0) {
			System.out.println("<<<<<<<<<<PP"
					+ phone_num.getText().toString().length());
			next_tv.setEnabled(true);
		} else {
			next_tv.setEnabled(false);

		}
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	public void onReigSMSS() {

		if (numBer == null || Country == null) {
			numBer = "+86";
			Country = "中国";
		}
		country_tv.setText(Country);
		country_code.setText(numBer);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.next_tv:

			onPhone();
			break;
		case R.id.country_rel:
			Intent intent2 = new Intent(this, RegionActivity.class);
			startActivityForResult(intent2, 101);
			break;
		default:
			break;
		}
	}

	public void onPhone() {
		phoneNumBer = phone_num.getText().toString();
		getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
		getYanzhengMaPresenter.getVerificationCode(phone_num.getText() + "",
				numBer, "2");
		count.start();

	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 101 && arg1 == 104) {
			String name = arg2.getStringExtra("province");
			String number = arg2.getStringExtra("number");
			country_tv.setText(name);
			country_code.setText(number);
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType().equals(
					GetYanzhengMaPresenter.YanzhengmaSuccessEvent)) {
				code = entity.getData().toString();
				String VD = code
						.substring(code.length() - 8, code.length() - 2);
				// ByAlert.alert("验证码："+VD);
				System.out.println("验证码：" + VD);
				toast(code);
				Intent intent = new Intent(SMSSendActivity.this,
						SMSReceiveActivity.class);
				intent.putExtra("phone", phoneNumBer);
				intent.putExtra("number", numBer);
				intent.putExtra("code", VD);
				intent.putExtra("type", "2");
				startActivity(intent);
				finish();
			} else if (entity.getEventType().equals(
					GetYanzhengMaPresenter.YanzhengmaFailEvent)) {
				toast("获取失败");
			}
		}
	}

}
